import Vue from "vue";
import Vuex from "vuex";
import { setItem, getItem } from "@/utils/localStorage.js";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    tokenInfo: getItem("tokenInfo") || {},
    photo: getItem("photo") || ""
  },
  mutations: {
    savePhoto(state, str) {
      state.photo = str;
      setItem("photo", state.photo);
    },
    // 获取后端的token对象
    saveToken(state, obj) {
      state.tokenInfo = obj;
      setItem("tokenInfo", state.tokenInfo);
    },
    // 只更新 token
    saveOnlyToken(state, newToken) {
      state.tokenInfo.token = newToken;
      setItem("tokenInfo", state.tokenInfo);
    },
    // 清空本地的token
    clearToken(state) {
      state.tokenInfo = {};
      setItem("tokenInfo", {});
    }
  },
  actions: {},
  modules: {}
});
